package com.jx.driving.protocol.terminal.codec;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;

/**
 * 直接数字读写器
 * 
 * @author shenliuyang
 *
 *         2016年7月29日下午6:45:17
 */
public class ByteArrayFieldReaderWriter extends FieldReaderWriter {

    public ByteArrayFieldReaderWriter(Field field, int size, int order) {
	super(field, size, order);
    }

    @Override
    public void read(InputStream in, Object to) throws IOException {
	byte[] b = new byte[size];
	in.read(b, 0, size);
	wrtietoField(to, b);
    }

    @Override
    public void write(OutputStream out, Object from) throws IOException {
	byte[] data = (byte[]) readFromField(from);
	if (data == null) {
	    data = new byte[size];
	}
	out.write(data);
    }

}